using System;
using System.Windows;
using System.Windows.Controls;
using CoreClasses;
using LayoutPlanning.CollaborationService;
using LayoutPlanning.Helpers;
using LayoutPlanning.ViewModel;
using Microsoft.Win32;
using Mvvm;

namespace LayoutPlanning.Model
{
    [Serializable]
    public class LayoutModel : BindableBase
    {
        private SerializableFrameworkElement _layout;
        private string _layoutName ;
        private double _height;
        private double _width;
        private string _pointcloud;
        public event PropertyValueChangedEventHandler<string> NameChanged;


        public SerializableFrameworkElement Layout
        {
            get { return _layout; }
            set
            {
                if(value == _layout)
                    return;
               
                _layout = value;
                
                //UpdateCloudLayout(_layoutName);
                RaisePropertyChanged();
                _layout.Element.LayoutUpdated += Element_LayoutUpdated;
               
                
                
            }
        }

        public void Initialize()
        {
            if (_layout?.Element == null)
                _layout =  new SerializableFrameworkElement(new UserControl() {Width = _width, Height = _height});
            _layout.Element.LayoutUpdated += Element_LayoutUpdated;
            _width = (_layout?.Element?.ActualWidth ??0) ==0? _width:_layout?.Element?.ActualWidth??0;
            _height = (_layout?.Element?.ActualHeight ??0) == 0 ?  _height: _layout?.Element?.ActualHeight ?? 0;
            RaisePropertiesChanged("Width", "Height", "Layout");
        }
        private void Element_LayoutUpdated(object sender, EventArgs e)
        {
            RaisePropertiesChanged("Width", "Height");
            _width = _layout?.Element?.Content== null?_width : _layout.Element.ActualWidth;
            _height = _layout?.Element?.Content == null ? _height : _layout.Element.ActualHeight;
        }

       
        public String LayoutName
        {
            get { return _layoutName==null ? "change this and save":_layoutName; }
            set
            {
                if(value == _layoutName)
                    return;
                
                  
                var oldname = _layoutName;
                _layoutName = value;
                
                UpdateCloudLayout(oldname);
                RaisePropertyChanged();
            }
        }

        private void UpdateCloudLayout(string oldlayoutname)
        {
            if(oldlayoutname == null)
                return;
#if DEBUG
            var service = new CollaborationService.ServiceContractClient("BasicHttpsBinding_IServiceContractDebug");
#else
                    var service = new  CollaborationService.ServiceContractClient("BasicHttpsBinding_IServiceContract");
#endif
            service.DeleteLayout(oldlayoutname);
            
            foreach (var projectViewModel in ViewModelLocator.Layouts.ProjectsInLayout)
            {
                if (oldlayoutname == projectViewModel.Project.LayoutName)
                {
                    service.DeleteProject(projectViewModel.Project.ProjectAreaName,oldlayoutname);
                }

            }
            ViewModelLocator.Main.SaveAreaCommand.Execute(null);
        }

        public double Width
        {
            get { return (_layout?.Element?.Content == null) ? _width : _layout?.Element?.ActualWidth ?? 0; }
            set
            {
                _width = value; 
                RaisePropertiesChanged();
            }
        }

        public double Height
        {
            get { return (_layout?.Element?.Content==null) ? _height : _layout?.Element?.ActualHeight ?? 0; }
            set
            {
                _height = value;
                RaisePropertyChanged();
            }
        }

        public string Pointcloud
        {
            get { return _pointcloud; }
            set
            {
                if(value == _pointcloud)
                    return;
                
                _pointcloud = value;
                UpdateCloudLayout(_layoutName);
                RaisePropertiesChanged();
                RaisePropertyChanged(nameof(PointCloudButtonText));
            }
        }

        public RelayCommand Browse=> new RelayCommand(() =>
        {
            var dialog = new OpenFileDialog();
            if (dialog.ShowDialog() ?? false)
            {
                Pointcloud = dialog.FileName;
            }
        });

        public string PointCloudButtonText => Pointcloud == null ? "Add Point Cloud" : "Change Point Cloud";


        
        
    }
}